package controller.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import pojos.admin.AdminInfo;
import pojos.common.ReplyMessage;
import service.AdminInfoService;
import service.CommonUtilsService;
import utils.InputVerifyUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 管理员信息控制器
 */
@Controller
@RequestMapping(value = {"/admin"})
public class AdminInfoController {

    private AdminInfoService adminInfoService;
    private CommonUtilsService utilsService;

    @Autowired
    public AdminInfoController(AdminInfoService adminInfoService, CommonUtilsService commonUtilsService) {
        this.adminInfoService = adminInfoService;
        this.utilsService = commonUtilsService;
    }

    /**
     * 修改用户名
     */
    @PostMapping("/frame/update-loginName-submit")
    @ResponseBody
    public ReplyMessage updateLoginName(String loginName, HttpServletRequest request){
        HttpSession session = request.getSession();
        AdminInfo adminInfo = (AdminInfo) session.getAttribute("AdminInfo");

        ReplyMessage replyMessage = adminInfoService.updateLoginName(adminInfo.getId(), loginName);
        if(replyMessage.getStatus() == 200){
            adminInfo.setName(loginName);
            session.setAttribute("AdminInfo", adminInfo);
        }

        return replyMessage;
    }

    /**
     * 修改密码
     */
    @PostMapping("/frame/update-password-submit")
    @ResponseBody
    public ReplyMessage updatePassword(String password, HttpServletRequest request){
        HttpSession session = request.getSession();
        AdminInfo adminInfo = (AdminInfo) session.getAttribute("AdminInfo");

        return adminInfoService.updatePassword(adminInfo.getId(), password);
    }

    /**
     * 修改手机号
     */
    @PostMapping("/frame/update-phone-submit")
    @ResponseBody
    public ReplyMessage updatePhone(String phone, String smsCode, String imgCode, HttpServletRequest request){
        HttpSession session = request.getSession();
        AdminInfo adminInfo = (AdminInfo) session.getAttribute("AdminInfo");

        ReplyMessage replyMessage = new ReplyMessage();

        if(InputVerifyUtils.isImgAuthCodeNotRight(session, imgCode)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("图片验证码不正确！");
            return replyMessage;
        }

        if(InputVerifyUtils.isPhoneNotRight(phone)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("手机号不正确！");
            return replyMessage;
        }

        if(InputVerifyUtils.isSmsCodeNotRight(session, smsCode)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("短信验证码不正确！");
            return replyMessage;
        }

        // 从Session中移除值
        session.removeAttribute("ImgAuthCode");
        session.removeAttribute("SmsAuthCode");

        replyMessage = adminInfoService.updatePhone(adminInfo.getId(), phone);
        if(replyMessage.getStatus() == 200){
            adminInfo.setPhone(phone);
            session.setAttribute("AdminInfo",adminInfo);
        }
        return replyMessage;
    }

    /**
     * 修改邮箱号
     */
    @PostMapping("/frame/update-email-submit")
    @ResponseBody
    public ReplyMessage updateEmail(){
        ReplyMessage replyMessage = new ReplyMessage();
        replyMessage.setStatus(400);
        replyMessage.setInfo("邮箱号暂不支持修改！");
        return replyMessage;
    }

    /**
     * 修改真实姓名
     */
    @PostMapping("/frame/update-realName-submit")
    @ResponseBody
    public ReplyMessage updateRealName(String name, HttpServletRequest request){
        HttpSession session = request.getSession();
        AdminInfo adminInfo = (AdminInfo) session.getAttribute("AdminInfo");

        ReplyMessage replyMessage = new ReplyMessage();
        name = name.trim();
        if(InputVerifyUtils.isRealNameNotRight(name)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("姓名不正确，请输入2~20个字符的中文名！");
            return replyMessage;
        }

        replyMessage = adminInfoService.updateRealName(adminInfo.getId(), name);
        if(replyMessage.getStatus() == 200){
            adminInfo.setRealName(name);
            session.setAttribute("AdminInfo",adminInfo);
        }
        return replyMessage;
    }

    /**
     * 修改管理员头像
     */
    @PostMapping("/frame/update-avatar-submit")
    @ResponseBody
    public ReplyMessage updateAvatar(MultipartFile avatar,HttpServletRequest request){
        HttpSession session = request.getSession();
        AdminInfo adminInfo = (AdminInfo) session.getAttribute("AdminInfo");

        ReplyMessage replyMessage = new ReplyMessage();

        if(InputVerifyUtils.isAvatarNotRight(avatar)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("文件不正确，请重新选择文件！");
            return replyMessage;
        }

        replyMessage = utilsService.alterAvatar(avatar, adminInfo.getAvatar(), adminInfo.getId(), 1);
        if(replyMessage.getStatus() == 200){
            adminInfo.setAvatar(replyMessage.getInfo());
        }
        return replyMessage;
    }
}
